package org.javaymw.springboot.controller;

import org.javaymw.springboot.base.BaseController;
import org.javaymw.springboot.dto.MemberDto;
import org.javaymw.springboot.param.MemberParam;
import org.javaymw.springboot.service.MemberService;
import org.javaymw.springboot.util.MessageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * 描述：
 * <p>
 * Author: 赵新国
 * Date: 2017/11/13 17:59
 */
@Controller
public class RegisterController extends BaseController {

    @Autowired
    private MemberService memberService;

    /**
     * 跳转到注册页面
     * @return
     */
    @RequestMapping("/register.html")
    public String register() {
        return "register";
    }

    /**
     * 注册会员
     * @param memberParam
     * @return
     */
    @RequestMapping(value = "/reg", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> reg(MemberParam memberParam) {
        Map<String, Object> retMap = null;
        if (memberService.getMemberCountByName(memberParam.getName()) > 0) {
            retMap = MessageUtils.error("该用户名已经存在");
        } else {
            MemberDto bizDto = new MemberDto();
            bizDto.setEmail(memberParam.getEmail());
            bizDto.setPassWord(memberParam.getPassWord());
            bizDto.setName(memberParam.getName());
            bizDto.setPhone(memberParam.getPhone());
            memberService.saveMember(bizDto);
            retMap = MessageUtils.success("注册成功");
        }
        return retMap;
    }
}
